﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SalaryChannel.SCPublicInforService;
using System.ServiceModel;

namespace SalaryChannel
{
    public partial class InforBox : UserControl
    {
        static Random rSb = new Random();

        /// <summary>
        /// Event of MouseLeftButtonDown
        /// </summary>
        public new event MouseButtonEventHandler MouseLeftButtonDown;

        public InforBox()
        {
            InitializeComponent();
            ServiceInstance.PublicInforClient.GetPublicInforCompleted += new EventHandler<GetPublicInforCompletedEventArgs>(DClient_GetPublicInforCompleted);
            //this.inforList.
          
               // this.ExposeBtn.IsEnabled = App.IsAdmin;
            ServiceInstance.PublicInforClient.ExposePublicInforCompleted += new EventHandler<ExposePublicInforCompletedEventArgs>(DClient_ExposePublicInforCompleted);

         
            
            try
            {

                ServiceInstance.PublicInforClient.GetPublicInforAsync(DateTime.Now.ToLongTimeString() + rSb.NextDouble().ToString());
                loading.Show();
            }
            catch (Exception)
            {
                
            }
            
        }

        void InforBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.MouseLeftButtonDown != null)
            {
                MouseLeftButtonDown(sender, e);
            }
        }

        

        void DClient_GetPublicInforCompleted(object sender, GetPublicInforCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null)
                {
                    if (e.Error is FaultException<ExceptionDetail>)
                    {
                        var fault = e.Error as FaultException<ExceptionDetail>;
                        App.MessageBox.ShowDialog(fault.Detail.ToString());
                    }
                }

                this.inforList.ItemsSource = e.Result;
            }
            catch(Exception ex)
            {
                App.MessageBox.ShowDialog("查询公告失败，"+ex.Message, Title.Error);
            }
            finally
            {
                loading.Stop();
            }
        }

        private void FreshBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                ServiceInstance.PublicInforClient.GetPublicInforAsync(DateTime.Now.ToLongTimeString() + rSb.NextDouble().ToString());
                loading.Show();
            }
            catch (Exception)
            {

            }

        }

        void ExposeAction()
        {
            try
            {
                int id = (this.inforList.SelectedItem as PublicInfor).ID;
                ServiceInstance.PublicInforClient.ExposePublicInforAsync(id, DateTime.Now.ToLongTimeString() + rSb.NextDouble().ToString());
            }
            catch
            {
                App.MessageBox.ShowDialog(Properties.Resource.ConnectServerFailed, Title.Error);

            }
        }

        private void ExposeBtn_Click(object sender, RoutedEventArgs e)
        {
            if (App.CurrentUser == null || !App.IsAuth(3))
            {
                App.MessageBox.ShowDialog(Properties.Resource.ExposeBulltinWarning, Title.Warning);
                return;
            }
            if (inforList.SelectedItem == null)
            {
                return;
            }
            App.MessageBox.ShowDialog(Properties.Resource.ExposeBulltinConfirm, new Action(ExposeAction), Title.Warning);

          
          
        }

        void DClient_ExposePublicInforCompleted(object sender, ExposePublicInforCompletedEventArgs e)
        {
            string msg = string.Empty;
            try
            {
                if (e.Result)
                {
                    msg = Properties.Resource.Succeeded;
                    try
                    {

                        ServiceInstance.PublicInforClient.GetPublicInforAsync(DateTime.Now.ToLongTimeString() + rSb.NextDouble().ToString());
                        loading.Show();
                    }
                    catch 
                    {

                    }
                }
                else
                {
                    msg = Properties.Resource.TryAgain;
                }
            }
            catch 
            {
               msg = Properties.Resource.TryAgain;
            }
            finally
            {
                App.MessageBox.ShowDialog(msg, Title.Information);
            }

        }

      

       
    }
}
